// pages/voting/voting.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiveDic: {
      // src_action_flag: '943398',
      // megagame_id: '39899659883286841'
    },
    url:'',
    page:1,
    dataArr:[],
    selectArr:[],
    searchText:'',
    canUplod: null,//是否可以上拉加载,
    remainingvotesCount:0,//剩余票数
    isLeader: false //是否是组长 组长可以一直投票 且投票数不局限于0 投完票票数不减
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("接收到的参数是obj=" + options.receiveDic);//此处打印出来的仅仅是字符串 需要解析，解析如下
    this.setData({
      receiveDic: JSON.parse(options.receiveDic)
    })
    this.setData({
      url: util.VoteLists + '?match_kge_id=' + this.data.receiveDic.megagame_id + '&src_action_flag=' + this.data.receiveDic.src_action_flag
    })
    this.getDetailLists();
  },

  //获取投票结果列表
  getDetailLists: function () {
    var that = this;
    var dic = {
      page: that.data.page,
      page_size_count: 12,
      key_words: that.data.searchText
    }
    util.publicRequestWithToken(
      that.data.url,
      'GET',
      dic,
      //sucess
      function (data) {
        if (data.code === 0) {
          //剩余票数
          that.setData({
            remainingvotesCount: data.detail.user_left_vote,
            isLeader: data.detail.is_leader
          })
          //成功
          if (that.data.page === 1) {
            //清空数据
            that.setData({
              dataArr: []
            })
          }
          var receiveArr = data.detail.results;
          //遍历该数组 并且赋值 未选中属性
          receiveArr.map((item, index) => {
            item.is_select = false
          })
          var newArr = that.data.dataArr.concat(receiveArr)
          if (data.detail.next) {
            that.setData({
              canUplod: true,
              page: that.data.page + 1,
              dataArr: newArr
            })
          } else {
            that.setData({
              canUplod: false,
              page: that.data.page,
              dataArr: newArr
            })
          }
          //拿到最大票数voteMaxCount
          if (that.data.dataArr.length > 0) {
            that.setData({
              voteMaxCount: that.data.dataArr[0].vote_number
            })
          } else {
            that.setData({
              voteMaxCount: 0
            })
          }
          console.log('最大票数为' + that.data.voteMaxCount)
        } else {
          wx.showToast({
            title: data.detail.msg,
            icon: 'none'
          })
        }
      },
      //fail
      function (error) { },
      function () {
        // complete
        wx.hideLoading()
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();

      }
    );
  },

  //批量投票
  batchVote:function(e){
    var that = this
    var dic = {
      vote_kge_ids: that.data.selectArr,
      match_kge_id: that.data.receiveDic.megagame_id,
    }
    util.publicRequestWithToken(
      util.BatchVoteForWork,
      'POST',
      dic,
      //sucess
      function (data) {
          console.log('投票成功')
          that.setData({
            page: 1,
            selectArr: []
          })
          this.getDetailLists()
      },
      //fail
      function (error) { },
      function () {
        // complete

      }
    );
  },

  selectToVote:function(e){
    var dic = this.data.dataArr[e.currentTarget.dataset.index]
    if(this.data.isLeader){
      //组长可以一直投票 且投票数不局限于0 投完票票数不减
      dic.is_select = !dic.is_select;
      this.data.dataArr.splice(e.currentTarget.dataset.index, 1, dic);//指定位置替换掉数据
      this.setData({
        dataArr: this.data.dataArr
      })

      //将已选中的存入id数组里
      var tempIDArr = new Array;
      //遍历该数组 并且赋值 未选中属性
      this.data.dataArr.map((item, index) => {
        if (item.is_select) {
          tempIDArr.push(item.id)
        }
      })
      this.setData({
        selectArr: tempIDArr
      })
    }else{
      //已经投过票
      if (dic.is_vote) {
        wx: wx.showToast({
          title: '不可重复投票',
          icon: 'none'
        })
        return;
      }
      //普通人员 先判断是否还有票
      //未选中
      if (!dic.is_select){
        //未选中
        if (this.data.remainingvotesCount == 0) {
          wx: wx.showToast({
            title: '对不起，当前你手中剩余的票数为0',
            icon: 'none'
          })
          return;
        }
      }
      dic.is_select = !dic.is_select;
      this.data.dataArr.splice(e.currentTarget.dataset.index, 1, dic);//指定位置替换掉数据
      this.setData({
        dataArr: this.data.dataArr
      })

      //将已选中的存入id数组里
      var tempIDArr = new Array;
      //遍历该数组 并且赋值 未选中属性
      this.data.dataArr.map((item, index) => {
        if (item.is_select){
          tempIDArr.push(item.id)
        }
      })
      this.setData({
        selectArr: tempIDArr
      })
      //计算余票
      if (dic.is_select) {
        this.setData({
          remainingvotesCount: this.data.remainingvotesCount -1
        })
      } else {
        this.setData({
          remainingvotesCount: this.data.remainingvotesCount + 1
        })
      }
    }
  },
  //下拉刷新
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      page: 1
    });
    this.getDetailLists();
  },

  //上拉加载
  onReachBottom: function () {
    console.log('上拉加载');
    if (this.data.canUplod) {
      this.getDetailLists();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})